package com.base.commons.web.utils;

import com.base.commons.utils.SecurityUtils;
import org.apache.commons.lang3.RandomStringUtils;

import java.security.NoSuchAlgorithmException;

/**
 * CMS安全工具类
 * Created by Irany(欧书剑) 2017/7/20 0020 15:43
 */
public class CmsSecurityUtils {

    /**
     * 对明文密码进行MD5加密
     * @param pwd
     * @return
     */
    public static String createUserPassword(String pwd){
        try{
            return SecurityUtils.createMd5Password(pwd, PropertyUtil.get("erp.sysuser.password.salt",""));
        }catch (NoSuchAlgorithmException e){
            throw new RuntimeException("对密码MD5加密失败");
        }
    }

    /**
     * 创建随机密码
     * @return  盐值
     */
    public static String[] generatePasswords(){
        String password=generatePassword();
        String encryptErpPassword=createUserPassword(password);
        return new String[]{password,encryptErpPassword};
    }

    /**
     * 生成随机密码
     * @return  随机密码
     */
    private static String generatePassword() {
        String password= RandomStringUtils.random(8,true,true);
        return password;
    }
}
